/**********************************************************************
 * Copyright: (C)2024 LingYun IoT System Studio
 * Author: LiJiahui<2199250859@qq.com>
 *
 * Description: UART communication module header.
 * Defines ring buffer, ISR, and data parsing interfaces.
 *
 * ChangeLog:
 * Version    Date       Author            Description
 * V1.0.0  2025.07.26    LiJiahui      Release initial version (modularized)
 * V1.1.0  2025.07.26    LiJiahui      Full bare-metal ring buffer and parser integration
 *
 ***********************************************************************/

#ifndef INC_UART_COMM_H_
#define INC_UART_COMM_H_

#include <stdbool.h>
#include <stdint.h>
#include "usart.h" // HAL 库的 UART 句柄
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "project_tasks.h"

// --- 配置宏定义 ---
#define RX_RING_BUFFER_SIZE         1024
#define PARSED_DATA_BUFFER_SIZE     256
#define PARSER_WORKING_BUFFER_SIZE  (RX_RING_BUFFER_SIZE + 1)

// --- UART 状态结构体 ---
typedef struct
{
	// AT 响应数据
	volatile bool at_response_ready;
	char parsed_at_response[PARSED_DATA_BUFFER_SIZE];
	volatile uint16_t parsed_at_response_len;

	// IPD 数据
	volatile bool received_ipd_data_ready;
	uint8_t received_ipd_data_buffer[PARSED_DATA_BUFFER_SIZE];
	volatile uint16_t received_ipd_data_len;
} UartCommData_t;


extern UartCommData_t uart_comm_data;

// 声明 UART 处理任务的句柄，用于任务通知
extern TaskHandle_t xUartProcessTaskHandle;

// --- 公共函数声明 ---
void uart_comm_init(UART_HandleTypeDef *huart);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
void uart_process_received_data(void);

// 临界区保护函数
#define enter_critical_section()    __disable_irq()
#define exit_critical_section()     __enable_irq()

#endif /* INC_UART_COMM_H_ */

